2023/12/23497字符

WeakSet & WeakMap

  • 内部储存对象地址不会影响垃圾回收
  • 只能添加对象
  • 不能遍历(不是可迭代对象),没有 size 属性

WeakSet

let arr = [1, 2, 3, 4, 5];
let set = new WeakSet();
set.add(arr);
console.log(set);  //--> [1, 2, 3, 4, 5]
arr = [];
console.log(set);  //--> No properties 垃圾回收机制,监测变化

WeakMap

let map = new WeakMap();
map.set({}, 234);  //--> key 值必须是数组或对象